import { Controller } from "../../mvc/core/Controller";
import { AutoGuideConfig } from "../resource/AutoGuideConfig";
import { AutoGuideType } from "../view/common/guide/AutoGuideType";
import { AutoGuideData } from "../view/common/guide/AutoGuideData";
import { IAutoGuideSteper } from "../view/common/guide/stepOperater/IAutoGuideSteper";
import { AutoGuideWindowSteper } from "../view/common/guide/stepOperater/AutoGuideWindowSteper";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { AutoGuideWinRectSteper } from "../view/common/guide/stepOperater/AutoGuideWinRectSteper";
import { AutoGuideRightDownIconSteper } from "../view/common/guide/stepOperater/AutoGuideRightDownIconSteper";
import { AutoGuideBackPackSteper } from "../view/common/guide/stepOperater/AutoGuideBackPackSteper";
import { AutoGuideTimerSteper } from "../view/common/guide/stepOperater/AutoGuideTimerSteper";
import { AutoGuideCloseWinSteper } from "../view/common/guide/stepOperater/AutoGuideCloseWinSteper";
import { AutoGuideStepRecorder } from "../view/common/guide/AutoGuideStepRecorder";
import { Window } from "../../component/window/Window";
import { DataEvent } from "../events/DataEvent";
import { CallLater } from "../../common/net/CallLater";
import { ClassTypesUtil } from "../view/common/ClassTypesUtil";
import { AutoGuideActivedType } from "../view/common/guide/AutoGuideActivedType";
import { TaskInfo } from "../model/TaskInfo";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
//class AutoGuideController
    
    export  class AutoGuideController extends Controller
    {
       

        /* internal  */nextFrameAfterWinClose(): void
        {
            var loc3=undefined;
            var loc4=0;
            var loc5=0;
            var loc6=0;
            var loc7=null;
            var loc8=null;
            if (this._lastCloseWin == null) 
            {
                return;
            }
            var loc1=this.recorder.doingStes;
            var loc2=[];
            var loc9=0;
            var loc10=loc1;
            for (loc3 in loc10) 
            {
                loc5 = int(loc3);
                if ((loc6 = int(loc1[loc3])) <= 0) 
                {
                    continue;
                }
                if ((loc7 = AutoGuideConfig.instance.getConfigByKeys(["mainId", "stepId"], [loc5, loc6])) == null || loc7.length == 0) 
                {
                    continue;
                }
                if ((loc8 = loc7[0]).guideType != AutoGuideType.Window) 
                {
                    continue;
                }
                if (String(loc8.guideTypeValue) != this._lastCloseWin.winTitleName) 
                {
                    continue;
                }
                loc2.push(loc5);
                this.winSteper.stop(loc8);
            }
            loc4 = 0;
            while (loc4 < loc2.length) 
            {
                this.recorder.delDoingId(int(loc2[loc4]));
                ++loc4;
            }
            this._lastCloseWin = null;
            return;
        }

        /* internal  */canIOperateStep(arg1: AutoGuideData): Boolean
        {
            if (arg1 == null) 
            {
                return false;
            }
            if (this.recorder.getDoneStep(arg1.mainId) >= arg1.stepId) 
            {
                return false;
            }
            if (arg1.stepId == this.recorder.getDoingStep(arg1.mainId)) 
            {
                return false;
            }
            var loc1=this.recorder.getDoneStep(arg1.mainId);
            if (loc1 + 1 != arg1.stepId) 
            {
                return false;
            }
            return true;
        }

        /* internal  */hasNextsNeedSaveStep(arg1: AutoGuideData): Boolean
        {
            var loc2=null;
            if (arg1 == null) 
            {
                return false;
            }
            var loc1=AutoGuideConfig.instance.getConfigByKeys(["mainId"], [arg1.mainId]);
            var loc3=0;
            var loc4=loc1;
            for(loc2 of loc4) 
            {
                if (!(loc2.stepId >= arg1.stepId && loc2.isSaveStep)) 
                {
                    continue;
                }
                return true;
            }
            return false;
        }

        /* internal  */checkAndSaveCacheName(arg1: AutoGuideData): void
        {
            if (arg1 == null || !arg1.isSaveStep) 
            {
                return;
            }
            this.recorder.saveStepDone(arg1.mainId, arg1.stepId);
            return;
        }

        /* internal  */get winSteper(): IAutoGuideSteper
        {
            if (this._winSteper == null) 
            {
                this._winSteper = new AutoGuideWindowSteper();
            }
            return this._winSteper;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.checkPlayerLevelupHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.checkTaskDoingHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.taskEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.taskUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.taskListGotHandler);
            Dispatcher.addEventListener(EventName.WindowShowed, this.checkWinShowedHandler);
            Dispatcher.addEventListener(EventName.WindowClosed, this.closeWinHandler);
            Dispatcher.addEventListener(EventName.AutoGuideStepEnd, this.stepEndHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            Dispatcher.addEventListener(EventName.SysSettingSuccess, this.onGotSysSetting);
            Dispatcher.addEventListener(EventName.AutoGuideActiveByEvent, this.activeByEventHandler);
            Dispatcher.addEventListener(EventName.AutoGuideBreakGuide, this.breakGuideHandler);
            Dispatcher.addEventListener(EventName.AutoGuideDelDoingRecord, this.delDoingRecordHandler);
            return;
        }

        /* internal  */get winRectSteper(): IAutoGuideSteper
        {
            if (this._winRectSteper == null) 
            {
                this._winRectSteper = new AutoGuideWinRectSteper();
            }
            return this._winRectSteper;
        }

        /* internal  */get iconSteper(): IAutoGuideSteper
        {
            if (this._iconSteper == null) 
            {
                this._iconSteper = new AutoGuideRightDownIconSteper();
            }
            return this._iconSteper;
        }

        /* internal  */get backPackSteper(): IAutoGuideSteper
        {
            if (this._backPackSteper == null) 
            {
                this._backPackSteper = new AutoGuideBackPackSteper();
            }
            return this._backPackSteper;
        }

        /* internal  */get timerSteper(): IAutoGuideSteper
        {
            if (this._timerSteper == null) 
            {
                this._timerSteper = new AutoGuideTimerSteper();
            }
            return this._timerSteper;
        }

        /* internal  */get closeWinSteper(): IAutoGuideSteper
        {
            if (this._closeWinSteper == null) 
            {
                this._closeWinSteper = new AutoGuideCloseWinSteper();
            }
            return this._closeWinSteper;
        }

        /* internal  */get recorder(): AutoGuideStepRecorder
        {
            if (this._recorder == null) 
            {
                this._recorder = new AutoGuideStepRecorder();
            }
            return this._recorder;
        }

        /* internal  */operateStep(arg1: AutoGuideData, arg2: Window=null): void
        {
            this.recorder.addDoingStep(arg1.mainId, arg1.stepId);
            var loc1=arg1.guideType;
            switch (loc1) 
            {
                case AutoGuideType.RightDownIcon:
                {
                    this.iconSteper.guide(arg1);
                    break;
                }
                case AutoGuideType.Window:
                {
                    this.winSteper.guide(arg1, arg2);
                    break;
                }
                case AutoGuideType.WindowRect:
                {
                    this.winRectSteper.guide(arg1, arg2);
                    break;
                }
                case AutoGuideType.BackPack:
                {
                    this.backPackSteper.guide(arg1);
                    break;
                }
                case AutoGuideType.Timer:
                {
                    this.timerSteper.guide(arg1);
                    break;
                }
                case AutoGuideType.CloseWin:
                {
                    this.closeWinSteper.guide(arg1, arg2);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        /* internal  */breakStep(arg1: AutoGuideData): void
        {
            this.recorder.delDoingId(arg1.mainId);
            var loc1=this.getSteper(arg1);
            if (loc1 == null) 
            {
                return;
            }
            loc1.stop(arg1);
            return;
        }

        /* internal  */getSteper(arg1: AutoGuideData): IAutoGuideSteper
        {
            var loc1=null;
            var loc2=arg1.guideType;
            switch (loc2) 
            {
                case AutoGuideType.RightDownIcon:
                {
                    loc1 = this.iconSteper;
                    break;
                }
                case AutoGuideType.Window:
                {
                    loc1 = this.winSteper;
                    break;
                }
                case AutoGuideType.WindowRect:
                {
                    loc1 = this.winRectSteper;
                    break;
                }
                case AutoGuideType.BackPack:
                {
                    loc1 = this.backPackSteper;
                    break;
                }
                case AutoGuideType.Timer:
                {
                    loc1 = this.timerSteper;
                    break;
                }
                case AutoGuideType.CloseWin:
                {
                    loc1 = this.closeWinSteper;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return loc1;
        }

        /* internal  */breakGuideHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as AutoGuideData;
            if (loc1 == null) 
            {
                return;
            }
            this.breakStep(loc1);
            return;
        }

        /* internal  */delDoingRecordHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as AutoGuideData;
            if (loc1 == null) 
            {
                return;
            }
            this.recorder.delDoingId(loc1.mainId);
            return;
        }

        /* internal  */stepEndHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as AutoGuideData;
            if (loc1 == null) 
            {
                return;
            }
            this.recorder.addDoneStep(loc1.mainId, loc1.stepId);
            this.recorder.delDoingId(loc1.mainId);
            this.recorder.addCurDonStep(loc1.mainId, loc1.stepId);
            this.checkAndSaveCacheName(loc1);
            CallLater.addCallBack(this.nextFrameWhenGuideEnd);
            return;
        }

        /* internal  */nextFrameWhenGuideEnd(): void
        {
            this.checkNextStepGuide();
            this.checkPreMainType();
            return;
        }

        /* internal  */onGotSysSetting(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.SysSettingSuccess, this.onGotSysSetting);
            this._systemSetterGot = true;
            this.checkAllInitedHandler();
            return;
        }

        /* internal  */activeByEventHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1["mainId"];
            var loc3=loc1["stepId"];
            var loc4;
            if ((loc4 = AutoGuideConfig.instance.getConfigByKeys(["mainId", "stepId"], [loc2, loc3])) == null || loc4.length == 0) 
            {
                return;
            }
            var loc5;
            var loc6=(loc5 = loc4[0]).clone();
            ClassTypesUtil.copyValue(loc6, loc1);
            this.operateStep(loc6);
            return;
        }

        /* internal  */sceneUpdateHandler(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            this._sceneInited = true;
            this.checkAllInitedHandler();
            return;
        }

        /* internal  */taskListGotHandler(arg1: Object): void
        {
            this._taskGoted = true;
            this.checkAllInitedHandler();
            return;
        }

        /* internal  */checkAllInitedHandler(): void
        {
            if (!this._systemSetterGot || !this._sceneInited || !this._taskGoted) 
            {
                return;
            }
            this.recorder.init();
            this.checkTaskDoingHandler(null);
            this.checkNextStepGuide(true);
            this.checkPreMainType(true);
            return;
        }

        /* internal  */checkPlayerLevelupHandler(arg1: Object): void
        {
            var loc3=null;
            var loc1=cache.role.roleInfo.level;
            var loc2=AutoGuideConfig.instance.getConfigByKeys(["guideActivedType", "guideActivedTypeValue"], [AutoGuideActivedType.LevelUp, loc1]);
            if (loc2 == null || loc2.length == 0) 
            {
                return;
            }
            var loc4=0;
            var loc5=loc2;
            for(loc3 of loc5) 
            {
                if (!this.canIOperateStep(loc3)) 
                {
                    continue;
                }
                if (!this.hasNextsNeedSaveStep(loc3)) 
                {
                    continue;
                }
                this.operateStep(loc3);
            }
            return;
        }

        /* internal  */checkNextStepGuide(arg1: Boolean=false): void
        {
            var loc2=null;
            var loc1=AutoGuideConfig.instance.getConfigByKeys(["guideActivedType"], [AutoGuideActivedType.PreGuideStep]);
            if (loc1 == null || loc1.length == 0) 
            {
                return;
            }
            var loc3=0;
            var loc4=loc1;
            for(loc2 of loc4) 
            {
                if (!this.canIOperateStep(loc2)) 
                {
                    continue;
                }
                if (arg1) 
                {
                    if (!this.hasNextsNeedSaveStep(loc2)) 
                    {
                        continue;
                    }
                }
                else if (this.recorder.getCurDoneStep(loc2.mainId) + 1 != loc2.stepId) 
                {
                    continue;
                }
                this.operateStep(loc2);
            }
            return;
        }

        /* internal  */checkPreMainType(arg1: Boolean=false): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=null;
            var loc7=null;
            var loc1=AutoGuideConfig.instance.getConfigByKeys(["guideActivedType"], [AutoGuideActivedType.PreMain]);
            if (loc1 == null || loc1.length == 0) 
            {
                return;
            }
            var loc8=0;
            var loc9=loc1;
            for(loc2 of loc9) 
            {
                if (!this.canIOperateStep(loc2)) 
                {
                    continue;
                }
                if ((loc3 = AutoGuideConfig.instance.getConfigByKeys(["mainId"], [int(loc2.guideActivedTypeValue)])) != null) 
                {
                    loc4 = loc3[0] as AutoGuideData;
                }
                if (loc4 == null) 
                {
                    continue;
                }
                loc5 = this.recorder.getDoneStep(loc4.mainId);
                if ((loc6 = AutoGuideConfig.instance.getConfigByKeys(["mainId", "stepId"], [loc4.mainId, loc5 + 1])) != null) 
                {
                    loc7 = loc6[0] as AutoGuideData;
                }
                if (!(loc7 == null) && this.hasNextsNeedSaveStep(loc7)) 
                {
                    continue;
                }
                if (arg1) 
                {
                    if (!this.hasNextsNeedSaveStep(loc2)) 
                    {
                        continue;
                    }
                }
                else if (loc7 != null) 
                {
                    continue;
                }
                this.operateStep(loc2);
            }
            return;
        }

        /* internal  */taskEndHandler(arg1: Object): void
        {
            var loc4=null;
            if (arg1 == null) 
            {
                return;
            }
            this.checkTaskDoingHandler(null);
            var loc1=arg1["task"] as TaskInfo;
            if (loc1 == null || loc1.sTask == null) 
            {
                return;
            }
            var loc2=loc1.sTask.code;
            var loc3=AutoGuideConfig.instance.getConfigByKeys(["guideActivedType"], [AutoGuideActivedType.TaskDoing]);
            var loc5=0;
            var loc6=loc3;
            for(loc4 of loc6) 
            {
                if (int(loc4.guideActivedTypeValue) != loc2) 
                {
                    continue;
                }
                if (loc4.isSaveStep == false) 
                {
                    continue;
                }
                this.recorder.saveStepDone(loc4.mainId, loc4.stepId);
            }
            return;
        }

        /* internal  */taskUpdateHandler(arg1: Object): void
        {
            var loc4=null;
            if (arg1 == null) 
            {
                return;
            }
            var loc1=arg1["task"] as TaskInfo;
            if (loc1 == null || loc1.sTask == null) 
            {
                return;
            }
            var loc2=loc1.sTask.code;
            var loc3=AutoGuideConfig.instance.getConfigByKeys(["guideActivedType"], [AutoGuideActivedType.TaskDoing]);
            var loc5=0;
            var loc6=loc3;
            for(loc4 of loc6) 
            {
                if (int(loc4.guideActivedTypeValue) != loc2) 
                {
                    continue;
                }
                if (loc4.isSaveStep == false) 
                {
                    continue;
                }
                if (loc1.status != ETaskStatus._ETaskStatusHadCompleted) 
                {
                    continue;
                }
                this.recorder.saveStepDone(loc4.mainId, loc4.stepId);
            }
            return;
        }

        /* internal  */checkTaskDoingHandler(arg1: Object): void
        {
            var loc2=null;
            var loc3=null;
            if (!this._sceneInited || !this._taskGoted) 
            {
                return;
            }
            NetDispatcher.removeCmdListener(ServerCommand.TaskRoleTaskGot, this.checkTaskDoingHandler);
            var loc1=AutoGuideConfig.instance.getConfigByKeys(["guideActivedType"], [AutoGuideActivedType.TaskDoing]);
            var loc4=0;
            var loc5=loc1;
            for(loc2 of loc5) 
            {
                if ((loc3 = cache.task.getDoingTaskByCode(int(loc2.guideActivedTypeValue))) == null) 
                {
                    continue;
                }
                if (!this.canIOperateStep(loc2)) 
                {
                    continue;
                }
                if (!this.hasNextsNeedSaveStep(loc2)) 
                {
                    continue;
                }
                this.operateStep(loc2);
            }
            return;
        }

        /* internal  */checkWinShowedHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Window;
            if (loc1 == null) 
            {
                return;
            }
            this._lastShowedWin = loc1;
            flash.utils.setTimeout(this.nextFrameAfterWinShowed, 200);
            return;
        }

        /* internal  */nextFrameAfterWinShowed(): void
        {
            var loc4=null;
            if (this._lastShowedWin == null) 
            {
                return;
            }
            var loc1=this._lastShowedWin;
            var loc2=loc1.winTitleName;
            var loc3=AutoGuideConfig.instance.getConfigByKeys(["guideType", "guideTypeValue"], [AutoGuideType.Window, loc2]);
            loc3 = loc3.concat(AutoGuideConfig.instance.getConfigByKeys(["guideType", "guideTypeValue"], [AutoGuideType.WindowRect, loc2]));
            if (loc3 == null || loc3.length == 0) 
            {
                return;
            }
            var loc5=0;
            var loc6=loc3;
            for(loc4 of loc6) 
            {
                if (loc4.guideActivedType == AutoGuideActivedType.Event) 
                {
                    continue;
                }
                if (loc4.guideActivedType == AutoGuideActivedType.TaskDoing) 
                {
                    if (cache.task.getDoingTaskByCode(int(loc4.guideActivedTypeValue)) == null) 
                    {
                        continue;
                    }
                }
                if (!this.canIOperateStep(loc4)) 
                {
                    continue;
                }
                if (!this.hasNextsNeedSaveStep(loc4)) 
                {
                    continue;
                }
                this.operateStep(loc4, loc1);
            }
            this._lastShowedWin = null;
            return;
        }

        /* internal  */closeWinHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Window;
            if (loc1 == null) 
            {
                return;
            }
            this._lastCloseWin = loc1;
            flash.utils.setTimeout(this.nextFrameAfterWinClose, 110);
            return;
        }

        private /* var */_canOpSteps: Map<any, any> /* flash.utils.Dictionary */;

        private /* var */_guidingSteps: Map<any, any> /* flash.utils.Dictionary */;

        private /* var */_sceneInited: Boolean=false;

        private /* var */_taskGoted: Boolean=false;

        private /* var */_systemSetterGot: Boolean=false;

        private /* var */_winRectSteper: IAutoGuideSteper;

        private /* var */_iconSteper: IAutoGuideSteper;

        private /* var */_backPackSteper: IAutoGuideSteper;

        private /* var */_timerSteper: IAutoGuideSteper;

        private /* var */_closeWinSteper: IAutoGuideSteper;

        private /* var */_winSteper: IAutoGuideSteper;

        private /* var */_lastShowedWin: Window;

        private /* var */_recorder: AutoGuideStepRecorder;

        private /* var */_lastCloseWin: Window;
    }
